//
//  HOWaterMeterViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/30/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOWaterMeterViewController.h"
#import "HOConstants.h"
#import "HOMeterListViewController.h"

@interface HOWaterMeterViewController ()
{
	UITextField *noInputBox;
	UITextField *valueInputBox;
}
@end

@implementation HOWaterMeterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self initTableView];
	[self initSubviews];
	[self registerKeyboard];
}

- (void)initSubviews
{
	noInputBox = [[UITextField alloc]
				  initWithFrame:CGRectMake(80,
										   (CELL_HEIGHT - 22) / 2,
										   self.view.frame.size.width - 80 - HO_HORIZONAL_PADDING,
										   22)];
	noInputBox.textColor = TEXT_COLOR_999999;
	noInputBox.font = [UIFont systemFontOfSize:15];
	noInputBox.clearButtonMode = UITextFieldViewModeWhileEditing;
	noInputBox.placeholder = NSLocalizedString(@"请输入表编号", @"请输入表编号");
//	noInputBox.layer.borderWidth = 1;
	
	valueInputBox = [[UITextField alloc]
				  initWithFrame:CGRectMake(80,
										   (CELL_HEIGHT - 22) / 2,
										   self.view.frame.size.width - 80 - HO_HORIZONAL_PADDING,
										   22)];
	valueInputBox.textColor = TEXT_COLOR_999999;
	valueInputBox.keyboardType = UIKeyboardTypeNumberPad;
	valueInputBox.clearButtonMode = UITextFieldViewModeWhileEditing;
	valueInputBox.font = [UIFont systemFontOfSize:15];
	valueInputBox.placeholder = NSLocalizedString(@"请输入表数值", @"请输入表数值");
//	noInputBox.layer.borderWidth = 1;
	
	baseTableview.tableFooterView = [self getFooterViewWithTitle:NSLocalizedString(@"提交", @"提交")];
	
}

- (void)hideKeyboard
{
	[valueInputBox resignFirstResponder];
	[noInputBox resignFirstResponder];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	[self hideKeyboard];
}


- (void)didClickFooterButton:(UIButton *)btn
{
	[self hideKeyboard];
	NSString *num = [self getTrimingCharaFromTextField:noInputBox];
	if(num.length == 0)
	{
		[self popupHintView:NSLocalizedString(@"请输入表编码", @"请输入表编码")];
		return;
	}
	NSString *value = [self getTrimingCharaFromTextField:valueInputBox];
	if(value.length == 0)
	{
		[self popupHintView:NSLocalizedString(@"请输入表数值", @"请输入表数值")];
		return;
	}
	
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_SubmitMetersValue,
								  kHO_FIELD_MeterType:[NSNumber numberWithInt:self.meterType],
								  kHO_FIELD_MeterNum:num,
								  kHO_FIELD_MeterValue:value}];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	switch (self.meterType)
 {
  		case METER_TYPE_WATER:
			[self setNavigationItemTitle:NSLocalizedString(@"水表", @"水表")];
			break;
		case METER_TYPE_PEAK_VALLEY_ELECTRIC:
			[self setNavigationItemTitle:NSLocalizedString(@"峰谷电表", @"峰谷电表")];
			break;
		case METER_TYPE_GAS:
			[self setNavigationItemTitle:NSLocalizedString(@"气表", @"气表")];
			break;
		case METER_TYPE_ELECTRIC:
			[self setNavigationItemTitle:NSLocalizedString(@"电表", @"电表")];
			break;
			
  	default:
			break;
	}
	[self displayRightButtonItemWithTitle:NSLocalizedString(@"抄表列表", @"抄表列表")];
}
- (void)rightButtonItemDidClick
{
	HOMeterListViewController *viewCtrl = [[HOMeterListViewController alloc] init];
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)dealloc
{
	[noInputBox release];
	[valueInputBox release];
	[super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CELL_HEIGHT;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return  IS_IPHONE_4_SCREEN ? 5 : SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
    }
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	cell.textLabel.font = [UIFont systemFontOfSize:15];
	cell.textLabel.textColor = TEXT_COLOR_666666;
	if(indexPath.section == 0)
	{
		cell.textLabel.text = NSLocalizedString(@"表编号", @"表编号");
		if((UITextField *)[cell viewWithTag:113] == nil)
		{
			noInputBox.tag = 113;
			[cell.contentView addSubview:noInputBox];
		}
	}
	else if(indexPath.section == 1)
	{
		cell.textLabel.text = NSLocalizedString(@"表数值", @"表数值");
		if((UITextField *)[cell viewWithTag:114] == nil)
		{
			valueInputBox.tag = 114;
			[cell.contentView addSubview:valueInputBox];
		}
	}
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_SubmitMetersValue])
	{
		self.view.userInteractionEnabled = NO;
		[self popupHintView:NSLocalizedString(@"提交成功！", @"提交成功！") inTime:1.5 needCallback:YES];
	}
}

- (void)hintDidHide
{
	self.view.userInteractionEnabled = YES;
	[self goBack];
}
#pragma mark -- tool
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
